using MediatR;
using WMS.Domain;
using WMS.ErrorCode;
using WMS.Infrasturcture.Interfaces;
using WMS.WebApiWrite.Application.Command;

namespace WMS.WebApiWrite.Application.Commandhandler
{
    /// <summary>
    /// 修改仓库信息命令处理器
    /// </summary>
    public class UpdateWarehouseCommandHandler : IRequestHandler<UpdateWarehouseCommand, ApiResult<bool>>
    {
        private readonly IBaseRepository<WarehouseInfo> warehouseRepository;

        /// <summary>
        /// 构造函数，注入仓库仓储
        /// </summary>
        public UpdateWarehouseCommandHandler(IBaseRepository<WarehouseInfo> warehouseRepository)
        {
            this.warehouseRepository = warehouseRepository;
        }

        /// <summary>
        /// 处理修改仓库信息命令
        /// </summary>
        public async Task<ApiResult<bool>> Handle(UpdateWarehouseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 验证仓库ID是否为空
                if (!request.WarehouseDTO.WarehouseID.HasValue || request.WarehouseDTO.WarehouseID.Value <= 0)
                {
                    return new ApiResult<bool>
                    {
                        Code = ApiResultEnum.修改失败,
                        Message = "仓库ID不能为空",
                        Data = false
                    };
                }

                // 查询仓库信息（只查询未删除的记录）
                var warehouseInfo = warehouseRepository.GetAll()
                    .Where(x => !x.IsDeleted)
                    .FirstOrDefault(x => x.WarehouseID == request.WarehouseDTO.WarehouseID.Value);

                if (warehouseInfo == null)
                {
                    return new ApiResult<bool>
                    {
                        Code = ApiResultEnum.修改失败,
                        Message = "仓库信息不存在",
                        Data = false
                    };
                }

                // 注意：仓库编码由系统自动生成，修改时不允许更改
                // 更新仓库信息（不更新仓库编码和删除标记）
                warehouseInfo.WarehouseName = request.WarehouseDTO.WarehouseName;
                warehouseInfo.Warehousecapacity = request.WarehouseDTO.Warehousecapacity;
                warehouseInfo.SupervisorId = request.WarehouseDTO.SupervisorId;
                warehouseInfo.WarehouseDeptId = request.WarehouseDTO.WarehouseDeptId;
                warehouseInfo.WarehousePhone = request.WarehouseDTO.WarehousePhone;
                warehouseInfo.WarehouseAddress = request.WarehouseDTO.WarehouseAddress;
                warehouseInfo.WarehouseState = request.WarehouseDTO.WarehouseState;
                warehouseInfo.WarehouseLockdown = request.WarehouseDTO.WarehouseLockdown;
                warehouseInfo.WarehouseContent = request.WarehouseDTO.WarehouseContent;
                warehouseInfo.DataStatus = request.WarehouseDTO.DataStatus; // 更新数据状态
                warehouseInfo.UpdateName = request.WarehouseDTO.OperatorName;
                warehouseInfo.UpdateTime = DateTime.Now;

                // 执行修改操作
                var result = await warehouseRepository.UpdateAsync(warehouseInfo);

                if (result > 0)
                {
                    return new ApiResult<bool>
                    {
                        Code = ApiResultEnum.修改成功,
                        Message = request.WarehouseDTO.DataStatus == 1 ? "保存草稿成功" : "修改仓库信息成功",
                        Data = true
                    };
                }
                else
                {
                    return new ApiResult<bool>
                    {
                        Code = ApiResultEnum.修改失败,
                        Message = "修改仓库信息失败",
                        Data = false
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult<bool>
                {
                    Code = ApiResultEnum.修改失败,
                    Message = $"修改仓库信息异常：{ex.Message}",
                    Data = false
                };
            }
        }
    }
}

